package controllers

import (
	"crypto/sha1"
	"github.com/astaxie/beego"
	"sort"
)

type MainController struct {
	beego.Controller
}

func (c *MainController) Get() {
	// MODIFIED AS wechat public
	params := make(map[string]string)

	token := "ABCDEFG"
	sig := c.Ctx.Input.Query("signature")
	timestamp := c.Ctx.Input.Query("timestamp")
	nonce := c.Ctx.Input.Query("nonce")
	echostr := c.Ctx.Input.Query("echostr")
	params["token"] = token
	params["timestamp"] = timestamp
	params["nonce"] = nonce
	keys := make([]string, len(params))
	i := 0
	for k,_ := range params {
		keys[i] = k
		i++
	}
	sort.Strings(keys)
	mysig := ""
	for _, key := range keys {
		mysig += key
	}
	res := sha1.New().Sum([]byte(mysig))
	ress := make(map[string]string)

	if sig == string(res) {
		ress["echostr"] = echostr
		c.Data["json"] = ress
	}
	c.ServeJSON()
}
